package com.party.admin.task;

import com.party.admin.biz.wechat.WechatBizMobileService;
import com.party.common.constant.WechatConstant;
import com.party.core.dao.read.initsubscribe.InitSubscribeDao;
import com.party.core.dao.write.crowdfund.AnalyzeWriteDao;
import com.party.core.model.wechat.WechatSubscribe;
import com.party.core.service.wechat.IWechatSubscribeService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 初始化/矫正活动报名者是否关注活动发布者的微信公众号
 */
@Component("quartzWechatSubscribe")
public class QuartzWechatSubscribe {

    @Autowired
    private WechatBizMobileService wechatBizMobileService;

    @Autowired
    private InitSubscribeDao initSubscribeDao;

    @Autowired
    private AnalyzeWriteDao analyzeWriteDao;

    @Autowired
    private IWechatSubscribeService wechatSubscribeService;

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    protected Logger logger = LoggerFactory.getLogger(QuartzWechatSubscribe.class);

    public void initSubscribe() {
//        logger.info(">>>>初始化/矫正活动报名者是否关注活动发布者的微信公众号开始!!!");
//        long start = System.currentTimeMillis();
//        // 找到所有众筹项目的订阅人员对应发布者token和项目id
//        List<Map<String, String>> list = initSubscribeDao.getAllOpenidAndToken(wechatOpenMemberService.getSystem().getAuthorizerAppid());
//
//        String openid = null;
//        String token = null;
//        String projectId = null;
//        String subscribe = null;
//        String appId = null;
//        for (Map<String, String> map: list) {
//            openid = map.get("openid");
//            token = map.get("token");
//            projectId = map.get("project_id");
//            appId = map.get("app_id");
//            try {
//                InfoResponse infoResponse = wechatBizMobileService.info(openid, token);
//                subscribe = infoResponse.getSubscribe();
//                if ("1".equals(subscribe)) {
//                    analyzeWriteDao.updateSubscribe(projectId, 1);
//                    QuartzWechatSubscribe.this.updateWechatSubscribe(openid, appId, true);
//                } else {
//                    analyzeWriteDao.updateSubscribe(projectId, 0);
//                    QuartzWechatSubscribe.this.updateWechatSubscribe(openid, appId, false);
//                }
//            } catch (Exception e) {
//                logger.error("调用微信接口获取是否关注失败, 错误信息为{}, token = {}, openid = {}",
//                        e, openid, token);
//            }
//        }
//        long end = System.currentTimeMillis();
//        logger.info("初始化微信是否关注成功!!!!!!!!! 耗时 {} 分钟" + ((end - start) / 1000 / 60));
    }

    private void updateWechatSubscribe(String openid ,String appId, boolean subscribe) {
        WechatSubscribe wechatSubscribe = wechatSubscribeService.get(appId, openid);
        if (null != wechatSubscribe) {
            if (subscribe) {
                wechatSubscribe.setStatus(WechatConstant.EVENT_TYPE_SUBSCRIBE);
            } else {
                wechatSubscribe.setStatus(WechatConstant.EVENT_TYPE_UNSUBSCRIBE);
            }
            wechatSubscribeService.update(wechatSubscribe);
        } else {
            wechatSubscribe = new WechatSubscribe();
            wechatSubscribe.setAppid(appId);
            wechatSubscribe.setOpenid(openid);
            if (subscribe) {
                wechatSubscribe.setStatus(WechatConstant.EVENT_TYPE_SUBSCRIBE);
            } else {
                wechatSubscribe.setStatus(WechatConstant.EVENT_TYPE_UNSUBSCRIBE);
            }
            wechatSubscribeService.insert(wechatSubscribe);
        }
    }
}
